package com.ccs.demo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import java.io.PrintStream;

@RequestMapping("/Request")
@RestController
public class RequestController {
    @RequestMapping("/getCookie1")
    public String getCookie1(HttpServletRequest request){
        Cookie[] cookies=request.getCookies();
        if(cookies!=null){
            Cookie[] arr=cookies;
            int len=cookies.length;

            for(int i=0;i<len;++i){
                Cookie cookie=arr[i];
                PrintStream var1000=System.out;
                String var10001=cookie.getName();
                var1000.println(var10001 + ":" +cookie.getValue() );
            }
        }
        return "获取cookie成功";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("quest") String quest){
        return "cookie:" + quest;
    }


    @RequestMapping ("setSession")
    public String setSession(HttpServletRequest request,String name,Integer age){
        HttpSession session= request.getSession();
        session.setAttribute("name",name);
        session.setAttribute("age",age);
        return "设置session成功";
    }


    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session=request.getSession();
        String name= (String) session.getAttribute("name");
        Integer age= (Integer) session.getAttribute("age");
        return " name:"+ name +" age:"+ age ;
    }

    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        String name=(String)session.getAttribute("name");
        Integer age=(Integer)session.getAttribute("age");
        return " name:"+name+" age:"+age;
    }

    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("name") String name,@SessionAttribute("age") Integer age){
        return " name:"+name+" age:"+age;
    }


    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgernt=request.getHeader("User-Agent");
        return "User-Agent:" + userAgernt;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader String userAgernt){
        return "user-Agernt:" + userAgernt;
    }



}
